package com.codechuan.demo.ui.registerVip;

import static com.codechuan.demo.common.Constant.context;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.demo.MainActivity;
import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.MyHttpUtil;

public class RegisterVipActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView reg_account;
    private TextView reg_password;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register_vip);
        // 获取按钮
        Button button = findViewById(R.id.reg_register);
        button.setOnClickListener(this);

        Button reg_return = findViewById(R.id.reg_return);
        reg_return.setOnClickListener(this);
        // 获取账号密码和卡密
        this.reg_account = findViewById(R.id.reg_account);

        this.reg_password = findViewById(R.id.reg_password);

        // 获取主线程
        handler = new Handler(getMainLooper());


    }


    @Override
    public void onClick(View view) {

        int id = view.getId();
        // 注册
        if (id == R.id.reg_register) {
            String reg_accountString = reg_account.getText().toString();
            String reg_passwordString = reg_password.getText().toString();
            if (reg_accountString.isEmpty()) {
                DialogUtil.showDialog(this, "账号不能为空!");
                return;
            }
            if (reg_passwordString.isEmpty()) {
                DialogUtil.showDialog(this, "卡密不能为空!");
                return;
            }
            new Thread(() -> {
                JSONObject result = MyHttpUtil.registerUser(reg_accountString, reg_passwordString);
                if (result == null){
                    Constant.handler.post(()->{
                        DialogUtil.showDialog(context, "服务器异常!");
                    });
                    return;
                }
                if (result.getString("code").equals("500")) {
                    handler.post(() -> DialogUtil.showDialog(this, result.getString("message")));
                    return;
                }
                if (result.getString("data").equals("1")) {
                    handler.post(() -> DialogUtil.showDialog(this, "注册成功."));
                } else {
                    handler.post(() -> DialogUtil.showDialog(this, "注册失败."));

                }
            }).start();
        }

        // 充值
        if (id == R.id.reg_return) {
            Intent intent = new Intent();
            intent.setClass(RegisterVipActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }
}